package com.yida.gwj.vote.service.impl;


import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.text.Convert;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.datasource.annotation.Slave;
import com.yida.gwj.common.constant.GwjConstants;
import com.yida.gwj.common.domain.GwjBaseCommunity;
import com.yida.gwj.common.domain.GwjBaseHouse;
import com.yida.gwj.common.domain.GwjBaseOwner;
import com.yida.gwj.common.service.IGwjBaseCommunityService;
import com.yida.gwj.common.service.IGwjBaseOwnerService;
import com.yida.gwj.common.utils.GwjConvertUtil;
import com.yida.gwj.vote.domain.GwjMaintenanceFundsBusiness;
import com.yida.gwj.vote.domain.dto.GwjBaseHouseOwnerDTO;
import com.yida.gwj.vote.mapper.GwjBusinessDataMapper;
import com.yida.gwj.vote.service.IGwjBusinessDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公维金业务数据Service业务层处理
 *
 * @author lqq
 * @date 2023-03-22
 */
@Service
@Slave
public class GwjBusinessDataServiceImpl implements IGwjBusinessDataService {

    @Resource
    private GwjBusinessDataMapper gwjBusinessDataMapper;

    @Resource
    private IGwjBaseOwnerService gwjBaseOwnerService;

    @Resource
    private IGwjBaseCommunityService gwjBaseCommunityService;


    /**
     * 根据业务ID查询所有数据（楼栋、房屋、业主）
     *
     * @param bizId 业务ID
     * @return /
     */
    @Override
    public GwjBaseHouseOwnerDTO getAllDataByBizId(Long bizId) {
        //查询业务小区
        GwjBaseCommunity baseCommunity = gwjBusinessDataMapper.selectGwjMaintenanceFundsBusinessCommunityByBizId(bizId);
        if (StringUtils.isNull(baseCommunity)) {
            throw new ServiceException("查无业务小区数据");
        }
        //查询业务房屋
        List<GwjBaseHouse> baseHouses = gwjBusinessDataMapper.selectGwjMaintenanceFundsBusinessHouseByBizId(bizId);
        //剔除公用户
        baseHouses.removeIf(h -> GwjConstants.YES.equals(h.getPublicFlag()));
        if (StringUtils.isEmpty(baseHouses)) {
            throw new ServiceException("查无基础房屋数据");
        }
        //查询基础业主
        List<GwjBaseOwner> baseOwners = new ArrayList<>();
        List<String> houseCodes = baseHouses.stream().map(GwjBaseHouse::getHouseCode).collect(Collectors.toList());
        //分段查询
        List<List<String>> codeList = GwjConvertUtil.partition(houseCodes, 100);
        for (List<String> code : codeList) {
            List<GwjBaseOwner> owners = gwjBaseOwnerService.selectGwjBaseListByGwjBaseHouseCodes(code.toArray(new String[0]), baseCommunity.getAreaId());
            if (StringUtils.isNotEmpty(owners)) {
                baseOwners.addAll(owners);
            }
        }
        if (StringUtils.isEmpty(baseOwners)) {
            throw new ServiceException("查无基础业主数据");
        }
        baseOwners.forEach(o -> {
            if (StringUtils.isAnyBlank(o.getOwnerPhone(), o.getOwnerName(), o.getCardNum())) {
                throw new ServiceException("请完善业主信息");
            }
        });
        GwjBaseHouseOwnerDTO dto = new GwjBaseHouseOwnerDTO();
        dto.setBaseHouses(baseHouses);
        dto.setBaseOwners(baseOwners);
        dto.setBaseCommunity(baseCommunity);
        return dto;
    }

    /**
     * 校验业务状态
     *
     * @param bizId 业务楼栋主键
     */
    @Override
    public GwjMaintenanceFundsBusiness checkMaintenanceFundsBusinessStatus(Long bizId) {
        GwjMaintenanceFundsBusiness business = gwjBusinessDataMapper.selectGwjMaintenanceFundsBusinessByBizId(bizId);
        if (StringUtils.isNull(business)) {
            throw new ServiceException("查无业务数据");
        }
        if (Convert.toInt(GwjConstants.YES) != business.getVoteFlag()) {
            throw new ServiceException("当前业务流程无法创建投票");
        }
        return business;
    }

    /**
     * 查询基础数据（小区信息）
     *
     * @param id 基础数据（小区信息）主键
     * @return 基础数据（小区信息）
     */
    @Override
    public GwjBaseCommunity selectGwjBaseCommunityByGwjBaseCommunityId(Long id) {
        return gwjBaseCommunityService.selectGwjBaseCommunityByGwjBaseCommunityId(id);
    }
}
